<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<div style="padding-top:5px;">
	<a class='myBtn roundBorder' onclick="addStaff()">任命工作人员</a>
	<table class="commonTable" id='staffTable'>
		<thead>
			<tr>
				<th>工号</th><th>工作人员</th><th>任职文号</th><th>任职时间</th><th>操作</th>
			</tr>
		</thead> 
		<tbody>
	 		<c:forEach items="${staffList}" var="staff">
	 			<tr id="labstaff${staff.id}">
	 				<td>${staff.teacher.staffNo }</td>
	 				<td>${staff.teacher.realName }</td>
	 				<td>${staff.positionNO }</td>
	 				<td><fmt:formatDate pattern="yyyy-MM-dd" value="${staff.startDate }"></fmt:formatDate></td>
	 				<td>
	 					<a href='javascript:void(0)' onclick='modLabStaff("${staff.teacher.id}")'>修改</a>&nbsp;
						<a href='javascript:void(0)' onclick='deleteLabStaff("${staff.id}")'>删除</a>
					</td>
	 			</tr>
	 		</c:forEach>
		</tbody>
	</table> 
</div>
<script>
	var staffList = new Array(); //新增或者修改的员工列表
	var deleteStaffs = new Array(); //删除的员工列表
	var staffCount = 0;
	function addStaff(staffId){
		
		var url = "expCenterTabController.do?staffTabEdit";
		if(staffId != undefined && staffId.length >10){
			url += "&id="+staffId;
		}
		
		console.log("url:"+url);
		 $.dialog({
			  	id:'xxx',
				content: 'url:'+url,
				lock : true,
				zIndex: getzIndex(), 
				parent:frameElement.api,
				width:480,
				height:350,
				title:'任命工作人员', 
				opacity : 0.3,
				cache:false,
			    ok: function(){
			    	iframe = this.iframe.contentWindow;
			    	var staff = iframe.getStaff();
			    	if(staff == null) return false;
			    	handleAddStaff(staff);
			    },
			    cancelVal: '关闭',
			    cancel: true /*为true等价于function(){}*/
		});
	}
	
	function addTempStaff(staffId,positionNO,startDate,endDate,workDetail){
		
		var url = "expCenterTabController.do?staffTabEdit";
		if(staffId != undefined && staffId.length >10){
			url += "&id="+staffId;
			url += "&positionNO="+positionNO;
			url += "&startDate="+startDate;
			url += "&endDate="+endDate;
			url += "&workDetail="+workDetail;
		}
		console.log("url:"+url);
		 $.dialog({
			  	id:'xxx',
				content: 'url:'+url,
				lock : true,
				zIndex: getzIndex(), 
				parent:frameElement.api,
				width:480,
				height:350,
				title:'任命工作人员', 
				opacity : 0.3,
				cache:false,
			    ok: function(){
			    	iframe = this.iframe.contentWindow;
			    	var staff = iframe.getStaff();
			    	if(staff == null) return false;
			    	handleAddStaff(staff);
			    },
			    cancelVal: '关闭',
			    cancel: true /*为true等价于function(){}*/
		});
	}
	
	function handleAddStaff(staff){
		console.log(staff);
		
		
		
		var index = isStaffListExist(staff.id);
		if(index != -1){ //更新 替换
			staffList[index] = staff;
			
		}else{ //新增

			if(staff.id.length < 10){staff.id =staffCount+"";}
			staffList[staffCount] = staff;
	 		staffCount++;
		}
		
		updateStaffTableRow(staff); 
		
	}
	
	//修改（新增）员工后，table中更新记录
	function updateStaffTableRow(staff){
		var id = staff.id;
 		
		if($('#labstaff'+staff.id).length > 0){
			$('#labstaff'+staff.id).html('');
		}else{
			$('#staffTable tbody').append("<tr id='labstaff"+id+"'></tr>");
		}

		//添加一行
		$('#labstaff'+staff.id).append("<td>"+staff.teacherNo+"</td>"+
		"<<td>"+staff.teacherName+"</td>"+
		"<td>"+staff.positionNO+"</td>"+
		"<td>"+staff.start+"</td>"+
		"<td><a href='javascript:void(0)' onclick='modTempLabStaff(\""+staff.teacherId+"\",\""+staff.positionNO+"\",\""+staff.start+"\",\""+staff.end+"\",\""+staff.workDetail+"\")'>修改</a>&nbsp;"+
		"<a href='javascript:void(0)' onclick='deleteLabStaff(\""+id+"\")'>删除</a></td>");
	} 
	
	
	function deleteLabStaff(id){
		$('#labstaff'+id).remove();
		for(var i =0;i<staffList.length;i++){
			if(staffList[i] != null && staffList[i].id == id){
				staffList[i] = null;
			}
		}
		
		if(id.length > 10){ //非临时id，才保存，传给服务器删除
			deleteStaffs.push(id);
		}
		
	}
	
	var currentModStaff;
	function modLabStaff(id){
		if(id.length < 10){ //id长度小于10位表示是临时id
			currentModStaff = getStaffFromList(id) ; 
		}else{
			currentModStaff = {id:id};	
		}
		 
 		
		addStaff(id);
	}
	
	function modTempLabStaff(id,positionNO,startDate,endDate,workDetail){
		addTempStaff(id,positionNO,startDate,endDate,workDetail);
	}
	
	var currentModStaff;
	function modLabStaff(id){
		if(id.length < 10){ //id长度小于10位表示是临时id
			currentModStaff = getStaffFromList(id) ; 
		}else{
			currentModStaff = {id:id};	
		}
		 
 		
		addStaff(id);
	}
	
	
	function getStaffFromList(id){
		for(var i =0;i<staffList.length;i++){
			if(staffList[i] != null && staffList[i].id == id){
				return staffList[i];
			}
		}
		return null;
	}
	
	/**
	* 判断员工修改新增数组中是否存在此条记录
	*/
	function isStaffListExist(id){
		for(var i =0;i<staffList.length;i++){
			if(staffList[i] != null && staffList[i].id == id){
				return i;
			}
		}
		return -1;
	}
	
</script>

<style>

</style>